/**
Copyright (c) 2008 New Gonzo <john@newgonzo.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/


package com.newgonzo.cannonball.css
{
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	import flash.utils.getQualifiedClassName;
	
	import com.newgonzo.cannonball.w3c.dom.css.ICSSRule;
	import com.newgonzo.cannonball.w3c.dom.css.ICSSValue;
	import com.newgonzo.cannonball.w3c.dom.css.ICSSStyleDeclaration;
		
	dynamic public class CSSStyleDeclaration extends Proxy implements ICSSStyleDeclaration
	{
		private var provider:CSSValueProvider;
		private var rule:ICSSRule;
		
		public function CSSStyleDeclaration(valueProvider:CSSValueProvider, parentRule:ICSSRule = null)
		{
			provider = valueProvider;
			rule = parentRule;
		}
		
		public function get length():uint
		{
			return provider.styleNames.length;
		}
		
		public function item(idx:uint):String
		{
			return provider.styleNames[idx] as String;
		}
		
		public function get parentRule():ICSSRule
		{
			return rule;
		}
		
		public function get cssText():String
		{
			return toCSSString();
		}
		
		public function set cssText(value:String):void
		{
			throw new Error("NOT_SUPPORTED");
		}
		
		public function getPropertyCSSValue(name:String):ICSSValue
		{
			return provider.getProperty(name);
		}
		
		public function removeProperty(name:String):String
		{
			return provider.removeProperty(name).cssText;
		}
		
		public function getPropertyValue(name:String):String
		{
			var value:ICSSValue = provider.getProperty(name);
			
			if(!value)
			{
				return "";
			}
			
			return value.cssText;
		}
		
		public function getPropertyPriority(name:String):String
		{
			return provider.getPropertyPriority(name);
		}
		
		public function setProperty(name:String, value:String, priority:String = ""):void
		{
			/*
			* TODO: This will probably require a reference to the CSSDocument
			* that owns this CSSStyleDeclaration. Should all CSS DOM nodes have a reference
			* to their owning CSSDocument?
			*/
			throw new Error("CSSStyleDeclaration.setProperty not implemented.");
		}
		
		public function toCSSString():String
		{
			var s:String = "";
			var name:String;
			var value:ICSSValue;
			var propNames:Array = provider.styleNames;
			
			for each(name in propNames)
			{
				value = provider.getProperty(name);
				s += name + ": " + value.cssText + ";\n";	
			}
			
			return s;
		}
		
		public function toString():String
		{
			return toCSSString();
		}
		
		/** 
		* Proxy implementation
		*
		*/
		flash_proxy override function getProperty(name:*):*
		{
			var propName:String = getCSSPropertyName((name as QName).localName);
			
			try
			{
				return getPropertyValue(propName);
			}
			catch(e:Error)
			{
				trace(e);	
			}
			
			return null;
		}
		
		flash_proxy override function setProperty(name:*, value:*):void
		{
			var propName:String = getCSSPropertyName((name as QName).localName);
			
			try
			{
				this.setProperty(propName, value.toString());
			}
			catch(e:Error)
			{
				trace(e);
			}
		}
		
		private function getCSSPropertyName(asPropName:String):String
		{
			var caps:Array = asPropName.match(/([A-Z])/g);
			
			if(!caps)
			{
				return asPropName;
			}
			
			var cssPropName:String = asPropName;
			var cap:String;
			
			for each(cap in caps)
			{
				cssPropName = cssPropName.split(cap).join("-" + cap.toLowerCase());
			}
			
			return cssPropName;
		}
	}
}
